<?php
/**
 * Created by PhpStorm.
 * User: xuechunxuan
 * Date: 2018/10/17
 * Time: 11:40
 */
namespace app\index\model;

use think\Db;

class User extends Base{
    public function __construct()
    {
       $this->setTableName("user");
    }

    /*
     * 根据1级id获取2级跟3级id  并且给每一级存在的id下级数量加1
     * @param $pid int 第1级id
     * */
    public function getPPath($pid){
        $this->incPNum($pid);
         //查询2级如果存在继续查询三级
        $pid2=Db::name($this->getTableName())->where(['id'=>$pid])->field("pid")->find()['pid'];
        $pPath="-$pid-";
        if($pid2>0){
            $this->incPNum($pid2);
          $pPath=$pPath."$pid2-";
          $pid3=Db::name($this->getTableName())->where(['id'=>$pid2])->field("pid")->find()['pid'];
          if($pid3>0){
              $this->incPNum($pid3);
              $pPath=$pPath."$pid3-";
          }
        }
        return $pPath;
    }

    /*
     * 给用户下级数量加1
     * */
    public function incPNum($userId){
        Db::name($this->getTableName())->where(['id'=>$userId])->setInc("subordinate_num");
    }
}